package com.wwwarehouse.resource_center.fragment.rules;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.rules.SelectAppObjAdapter;
import com.wwwarehouse.resource_center.bean.rules.StocksBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/10/18.
 *         <p/>
 *         选择应用对象列表
 */

public class SelectObjectListFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    SelectAppObjAdapter mAdapter;
    private int pos;
    private int numOfPage;
    public static final int GET_DISTRIBUT_ABLE_STOCKS = 1;  //获取未分配的应用对象
    private List<StocksBean.ListBean> beanList;
    private String businessId;
    private ArrayList<StocksBean.ListBean> mSelectList = new ArrayList<>();
    private ArrayList<String> businessIdList = new ArrayList<>();//业务单位集合
    @Override
    protected int getContentId() {
        return R.layout.release_object_list;
    }
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        if (b!=null){
            businessId = b.getString("businessId");
            pos = b.getInt("pos");
            numOfPage = b.getInt("num");
            mSelectList = b.getParcelableArrayList("mStocksListBean");
            businessIdList.add(businessId);
        }
        this.requestDatas();
    }

    @Override
    public void requestDatas() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> baseQueryMap = new HashMap<>();
        baseQueryMap.put("page",pos);
        baseQueryMap.put("size",numOfPage);
        baseQueryMap.put("sort","UPDATE_TIME DESC");
        map.put("businessIdList", businessIdList);
        map.put("baseQuery", baseQueryMap);
        httpPost(ResourceConstant.GET_DISTRIBUT_ABLE_STOCKS,map,GET_DISTRIBUT_ABLE_STOCKS,false,"");
    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == GET_DISTRIBUT_ABLE_STOCKS) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                StocksBean stocksBean  =   JSON.parseObject(commonClass.getData().toString(), StocksBean.class);
                beanList = stocksBean.getList();
                if (beanList!=null&&beanList.size()>0&&mSelectList!=null&&mSelectList.size()>0){
                    for (int i = 0; i < mSelectList.size(); i++) {
                        for (int j = 0; j < beanList.size(); j++) {
                             if (mSelectList.get(i).getStockUkid().equals(beanList.get(j).getStockUkid())){
                                 beanList.get(j).setSelect(true);
                             }
                        }
                    }
                }

                mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
                    @Override
                    public boolean canScrollHorizontally() {
                        return false;
                    }

                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }

                });
                final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        observer.removeGlobalOnLayoutListener(this);
                        if (mAdapter == null) {
                            mAdapter = new SelectAppObjAdapter(mActivity, beanList, mRvContent.getHeight(), mRvContent.getWidth(), numOfPage, pos);
                        }
                        mRvContent.setAdapter(mAdapter);
                    }
                });
            }
        }

    }

    @Override
    public void onReLoad(int code) {

    }
}
